/**
 * Created by lyf on 2017/6/20.
 */


'use strict'

let node_ssh = require('node-ssh');
const serviceConfig = require('../conf/service.json');
let ssh = new node_ssh();

module.exports = function deployKong(kong , zookeeper , tsungMaster) {
    return new Promise (async function(resolve,reject){
        console.log(kong);
        for ( let i = 0 ; i < kong.length ; i++){
            await ssh.connect({
                host: kong[i][1],
                username: serviceConfig.user ,
                password: serviceConfig.password
            });
            const result = await ssh.execCommand (
                ` docker run -d ` +
                ` --log-driver=fluentd --log-opt "fluentd-address=$(ifconfig eth0 | grep inet | awk '{print $2}'):24224"` +
                ` -e TZ="Asia/Shanghai" `+
                ` -v /etc/localtime:/etc/localtime:ro ` +
                ` -e "SERVER_IP=$(ifconfig eth0 | grep inet | awk '{print $2}')" ` +
                ` -e "ZOOKEEPER_ADDR=${zookeeper[0][0]}:2181" ` +
                ` -e "LIMIT_CONNECTION=10000" ` +
                ' --add-host ${HOSTNAME}:127.0.0.1 ' +
                ` --net=host ` +
                ` --name kong-smartstack-10-2 ` +
                ` foreveross/kong-nocache-ngxsh `
            );

            //result.stdout ? resolve() : reject ();

            console.log(`kong:${kong[i][1]}`);
            console.log("-----------------------------");
            console.log(result.stdout);
            console.log(result.stderr);
            console.log("-----------------------------");

            // start
            await ssh.execCommand (`/root/test/ab-tsung/masterSlaveMode/start_target.sh ${tsungMaster[0][0]}`);
            ssh.dispose();
            resolve();

        }
    })

}
